package com.tuyue.police.controller.vip;

import com.tuyue.police.bean.RuleBean;
import com.tuyue.police.biz.IRuleBiz;
import com.tuyue.police.biz.ISmsBiz;
import com.tuyue.police.biz.IUserBiz;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.Rule;
import com.tuyue.police.pojo.User;
import com.tuyue.police.util.Result;
import com.tuyue.police.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/14.
 * @Modified By:
 */
@RestController
@RequestMapping("vip/rule/")
public class RuleController {

    @Autowired
    private IRuleBiz ruleBiz;

    @Autowired
    private ISmsBiz smsBiz;

    @Autowired
    private IUserBiz userBiz;
    /**
     * 规则列表
     * @param userId
     * @return
     */
    @GetMapping("list/{userId}")
    public Result list(@PageableDefault(size = 10, sort = {"ruleId"}, direction = Sort.Direction.DESC)
                                   Pageable pageable, @PathVariable Long userId){
        Page<Rule> ruleList = ruleBiz.findAllByUserId(userId,pageable);
        return ResultUtil.success(ruleList);
    }

    /**
     * 规则详情
     * @param rId
     * @return
     */
    @GetMapping("findById/{rId}")
    public Result findById(@PathVariable Long rId){
        RuleBean ruleBean = ruleBiz.findById(rId);
        return ResultUtil.success(ruleBean);
    }


    /**
     * 新增规则
     * @param ruleBean
     * @return
     */
    @PostMapping("add")
    public Result add(@RequestBody RuleBean ruleBean){
        if (StringUtils.isEmpty(ruleBean.getRuleUserId())) {
            throw new MyException(ResultEnum.USER_NOTNULL);
        }
        if (StringUtils.isEmpty(ruleBean.getRuleDispenseId())) {
            throw new MyException(ResultEnum.DISPENSEWAY_NOTNULL);
        }
        RuleBean bean = ruleBiz.add(ruleBean);
        return ResultUtil.success(bean);
    }


    @GetMapping("send/{phone}")
    public Result send(@PathVariable String phone){
        if (phone.length()!=11) {
            throw new MyException(ResultEnum.SMS_TELERROR);
        }
        User user = userBiz.findByPhome(phone);
        if (user==null) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        boolean b = smsBiz.sendSms( phone);
        if(b){
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultEnum.SMS_SENDERROR);
    }

    @GetMapping("verify/{phone}")
    public Result verify(@PathVariable String phone,String code){
        User user = userBiz.findByPhome(phone);
        if (user==null) {
            throw new MyException(ResultEnum.USER_ERROR);
        }
        boolean b = smsBiz.verifySms( phone,code);
        if(b){
            return ResultUtil.success(user);
        }
        return ResultUtil.error(ResultEnum.SMS_CODEERROR);
    }

}
